elastic search2 Min Read

Common used Elastic Search queries

Gorav Singal

April 18, 2018

TL;DR

Quick reference for everyday Elasticsearch operations: list indices, search documents, match queries, bool filters, and aggregations with curl examples.

Common used Elastic Search queries

Listing down the commonly used Elastic Search queries.

To see all available Index names

GET /_cat/indices?v

Output:

``` health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana HObWTCY_SZOWqShWiQ1vRA 1 1 8 3 55.5kb 27.7kb ```

To see all the Documents for an Index

GET INDEX_NAME/_search
{
  "query": {
    "match_all": {}
  }
}

To see all the Documents for an Index and a type

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match_all": {}
  }
}

Insert/Update a document

PUT INDEX_NAME/TYPE_NAME/6
{
  "name": "name2",
  "createdAt": "2017-07-27T08:38:48.276Z"
}

Delete a document by Id

DELETE INDEX_NAME/TYPE_NAME/ID

Get only specified attributes of document in result

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["ATTRIBUTE_NAME1", "ATTRIBUTE_NAME2"]
}

Get result within Date ranges specified

You can get search results within two dates specified.

Assuming: createdAt is the attribute which has date of creation of document.

Here,
gte means, greater than or equal to
lte means, less than or equal to

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "range": {
      "createdAt": {
        "gte": "24/07/2017-13/02/12",
        "lte": "26/07/2017-15/02/05",
        "format": "dd/MM/yyyy-HH/mm/ss"
      }
    }
  }
}

To get last updated record based on date

You have number of records. When you search by a unique identifier, you get all of them. But, in many cases, you might be needing only latest one or last few records.

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "NAME_ATTRIBUTE": "My identifier for name attribute"
    }
  },
  "size": 10,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Above code will give 10 records of criteria mentioned. If I need only latest one record. I need to mention size to 1.

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "NAME_ATTRIBUTE": "My identifier for name attribute"
    }
  },
  "size": 1,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Search on nested attribute

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "data.key": "My value"
    }
  }
}

Search on nested attribute, and get latest one

GET INDEX_NAME/TYPE_NAME/_search
{
  "query": {
    "match": {
      "data.key": "My value"
    }
  },
  "size": 1,
  "sort": [
    {
      "createdAt": {
        "order": "desc"
      }
    }
  ]
}

Delete all documents from a Type

DELETE INDEX_NAME/TYPE_NAME/

Delete an Index completely

DELETE INDEX_NAME

Search for the Existence of a Particular Attribute

You want to search for all the documents where this particular attribute is not present.

Note: I’m not talking about its value. Just presence.

GET /index_name/type_name/_search
{
  "query" : {
        "bool": {
          "must_not": [
            { "exists": { "field": "data.attrs.syncIssue" }}
          ]
        }
    }
}
Share

Related Posts

ElasticSearch: Validation Failed: 1: script or doc is missing

ElasticSearch: Validation Failed: 1: script or doc is missing

While dealing with ELastic Search documents, you faced this issue while updating…

ElasticSearch - Update a document and change value of a key

ElasticSearch - Update a document and change value of a key

Thanks for reading.

How to sync Mongodb data to ElasticSearch by using MongoConnector

How to sync Mongodb data to ElasticSearch by using MongoConnector

Introduction This post is about syncing your mongodo database data to…

How to take Backup from MongoDB and Restore to MongoDB

How to take Backup from MongoDB and Restore to MongoDB

This will take backup of your passed database name, to the passed folder. It…

How to connect Php docker container with Mongo DB docker container

How to connect Php docker container with Mongo DB docker container

Goto your command terminal. Type: This will expose port: 27017 by default. You…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…